import React from 'react';
import Animate from 'rc-animate';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { Link } from 'react-router';

import Container from '../components/Container';

import * as Shell from '../common/Shell';
import * as Util from '../common/Util';
import * as LoginActions from '../actions/LoginActions';

import * as Storage from '../common/Storage';
import * as Key from '../config/Key';

class AuthApp extends React.Component {

  constructor(props) {
    super(props);
  }

  onGetUserInfoSuccess(data){
    //如果获取到用户登录凭据，则隐式登录
    if (data && data.phoneNo && data.loginPw) {
      let { phoneNo:phone, loginPw:password } = data;
      this.props.loginAction.login({phone, password});
    }else{
      this.redirectToPage("/Account");
    }
  }

  redirectToPage(pageName){

    let timer = setTimeout(function(){
      clearTimeout(timer);
      this.props.history.pushState(null, pageName);
    }.bind(this), 10);
  }

  componentDidMount(){

    let { history } = this.props;
    
    //获取壳子保存的用户凭据信息，进行身份信息的验证
    Shell.callShellMethod(
      "getUserInfoPlugin", 
      this.onGetUserInfoSuccess.bind(this)
    );

    //获取认证后指向页面
    let redirectPage = Util.getUrlParam("p");
    if (redirectPage && redirectPage.length) {
      this.successPage = `/${redirectPage}`;
      Storage.sessionStorageService.set(Key.redirectKey, this.successPage);
    }else{
      this.successPage = "/Product";
    }
  }

  componentDidUpdate(){
    let { login_status = 0, message = "" } = this.props.login;
    //登录失败
    if (login_status === -1) {
        this.redirectToPage("/Account");
    }else if(login_status === 1){
      this.redirectToPage(this.successPage);
    }
  }

  render() {
    return (
        <Container 
            isOffsetY = {false}
            isHeader={false} 
            isScroll={false}
            isFooter={false}
            isAuth = {false} 
            className="account-container">
        </Container>
    );
  }
}

export default connect(state => ({
    login : state.login
}), dispatch => ({
    loginAction: bindActionCreators(LoginActions, dispatch)
}))(AuthApp);
